package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.androidnetframe.utils.UIUtils;
import com.mmlc.inter.InterFragment;
import com.mmlc.meimei.lichengkongjian.App;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.customview.PhoneEditText;
import com.mmlc.network.filter.LoginRequestFilter;
import com.mmlc.network.responsebean.LoginResponseBean;
import com.mmlc.utils.EditInputUtils;
import com.mmlc.utils.ToastUtils;
import com.mmlc.widget.TitleBarView;

public class LoginFragment extends InterFragment implements OnClickListener {

    @Override
    protected int initResourceID() {
        return R.layout.fragment_login;
    }

    PhoneEditText et_phoneNumber;
    EditText et_password;
    Button bt_login;
    ViewGroup rootView;
    TitleBarView tb_titleBar;
    TextView tv_register;
    TextView tv_forgetPassword;
    LinearLayout ll_qq;
    LinearLayout ll_weixin;
    LinearLayout ll_weibo;

    @Override
    protected void initView() {
        et_phoneNumber = (PhoneEditText) view.findViewById(R.id.et_phoneNumber);
        et_password = (EditText) view.findViewById(R.id.et_password);
        bt_login = (Button) view.findViewById(R.id.bt_login);
        rootView = (ViewGroup) view.findViewById(R.id.rootView);
        tb_titleBar = (TitleBarView) view.findViewById(R.id.tb_titleBar);
        tv_register = (TextView) view.findViewById(R.id.tv_register);
        tv_forgetPassword = (TextView) view.findViewById(R.id.tv_forgetPassword);
        ll_qq = (LinearLayout) view.findViewById(R.id.ll_qq);
        ll_weixin = (LinearLayout) view.findViewById(R.id.ll_weixin);
        ll_weibo = (LinearLayout) view.findViewById(R.id.ll_weibo);

        processData();
    }

    public void processData() {
        //处理标题栏
        tb_titleBar.setCenterName("个人中心");
        //登陆
        processLogin();
        //注册
        tv_register.setOnClickListener(this);
        //忘记密码
        tv_forgetPassword.setOnClickListener(this);
        //QQ
        ll_qq.setOnClickListener(this);
        //微信
        ll_weixin.setOnClickListener(this);
        //微博
        ll_weibo.setOnClickListener(this);
    }

    private void processLogin() {
        bt_login.setOnClickListener(this);
    }


    @Override
    protected void initExtra() {
        super.initExtra();

        /*umShareAPI = MainActivity.umShareAPI;*/
        //UMShareAPI.get(getActivity());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login:
                requestLogin();
                break;
            case R.id.tv_register:
                UIUtils.simpleStartActivity(getActivity(), RegisterAccount.class);
                break;
            case R.id.ll_qq:
                break;
            case R.id.tv_forgetPassword:
                Intent intent = new Intent(getActivity(), ForgetPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_weixin:
                break;
            case R.id.ll_weibo:
                break;
        }
    }

    private void requestLogin() {
        //检查手机号的合法性
        boolean isPhone = EditInputUtils.checkedPhoneNumber(et_phoneNumber.getText().toString());
        if (!isPhone) {
            ToastUtils.showToast(getActivity(), "请输入正确的手机号");
            return;
        }
        //检查密码是否符合要求
//        boolean isPassword = EditInputUtils.checkPassword(getActivity(), et_password.getText().toString());
//        if (!isPassword) {
//            ToastUtils.showToast(getActivity(), "密码只能以字符开头，其后跟字母，数字，下划线");
//        }
        //发送登陆的网络请求
        LoginRequestFilter filter = new LoginRequestFilter(context);
        filter.requestBean.method = "login";
        filter.requestBean.phone = et_phoneNumber.getInputText();
        filter.requestBean.password = et_password.getText().toString();
        filter.setDebug(false);//表示请求真正的服务端接口
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<LoginResponseBean>() {

            @Override
            public void onSuccess(LoginResponseBean result) {
                //缓存用户信息
                App.user.setPassword(et_password.getText().toString());
                /*App.user.saveNickName();
                App.user.setPassword();*/
                //进入个人中心列表界面
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {

            }
        });
    }



}
